package com.lhl.miniapp.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.json.JSONUtil;
import com.lhl.log.annotation.Log;
import com.lhl.wx.miniapp.config.WxMaConfiguration;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @className: com.lhl.miniapp.controller.WxMaUserController
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-11-30
 **/
@Api(tags = "WxMaUserController", produces = "WxMaUserController")
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/wx/user/{appId}")
public class WxMaUserController {
    /**
     * 登陆接口
     */
    @GetMapping("/login")
    public String login(@PathVariable String appId,
                        @RequestParam(name = "code", required = false) String code) {
        if (StringUtils.isBlank(code)) {
            return "empty jscode";
        }

        final WxMaService wxService = WxMaConfiguration.getMaService(appId);

        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            log.info(session.getSessionKey());
            log.info(session.getOpenid());
            //TODO 可以增加自己的逻辑，关联业务相关数据
            return JSONUtil.toJsonStr(session);
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
            return e.toString();
        }
    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @GetMapping("/info")
    public String info(@PathVariable String appId,
                       @RequestParam(name = "sessionKey", required = false) String sessionKey,
                       @RequestParam(name = "signature", required = false) String signature,
                       @RequestParam(name = "rawData", required = false) String rawData,
                       @RequestParam(name = "encryptedData", required = false) String encryptedData,
                       @RequestParam(name = "iv", required = false) String iv) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appId);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }

        // 解密用户信息
        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);

        return JSONUtil.toJsonStr(userInfo);
    }

    /**
     * <pre>
     * 获取用户绑定手机号信息
     * </pre>
     */
    @GetMapping("/phone")
    @Log("获得手机号码")
    public String phone(@PathVariable String appId,
                        @RequestParam(name = "sessionKey", required = false) String sessionKey,
                        @RequestParam(name = "signature", required = false) String signature,
                        @RequestParam(name = "rawData", required = false) String rawData,
                        @RequestParam(name = "encryptedData", required = false) String encryptedData,
                        @RequestParam(name = "iv", required = false) String iv) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appId);
        log.debug("encryptedData=={}",encryptedData);
        log.debug("iv=={}",iv);

        // 用户信息校验
        /*if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return "user check failed";
        }*/

        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);

        return JSONUtil.toJsonStr(phoneNoInfo);
    }
}
